﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VolumetricLines;

[RequireComponent(typeof(BoxCollider))]
public class SRay : STransform
{
    BoxCollider2D box = null;
    VolumetricLineBehavior line = null;
    public override void SInit()
    {
        box = GetComponent<BoxCollider2D>();
        line = GetComponentInChildren<VolumetricLineBehavior>();
        base.SInit();

    }
    [PureValueAtrribute]
    public float DamageGap = 1f;
    [PureValueAtrribute]
    public int Power = 100;
    [DynamicValueAtrribute]
    public Vector2 Size
    {
        set
        {
            box.size = value;
            line.EndPos = new Vector3(-value.x / 2, 0, 0);
            line.StartPos = new Vector3(value.x / 2, 0, 0);
            line.LineWidth = value.y;

        }
        get
        {
            return box.size;
        }
    }
    float lastDamageTime;
    public override void SStart()
    {
        base.SStart();
        lastDamageTime = float.NegativeInfinity;
    }
    public override void SFixedUpdate()
    {
        base.SFixedUpdate();
        if (Time.realtimeSinceStartup - lastDamageTime > DamageGap)
        {
            var objs=Entity.entityAPI.sharedCollisionManager.GetOverlapBox(this.LocalPosition, Size, this.LocalRotation+(this.LocalIsFliped?180:0), this.gameObject.layer);
            foreach(var x in objs)
            {
                if (x.Item1 != BelongingSystem.Default || this.Entity.Data.IsIgnored(x.Item2.left())==false)
                {
                    Debug.Log(x.Item2);

                    Entity.entityAPI.sharedCollisionManager.MagicAttack(x.Item1, x.Item2, Power);
                }
            }
        }
        Debug.Log(transform.position);
    }
}